#region Imported Namespaces
using System;
using System.Collections;
using System.Web.Mail;
using System.Runtime.InteropServices;
using PHSRAG.WebControls;
using Constants = PHSRAG.Insight21.InsightUtilities.Constants;
using UserContext = PHSRAG.Insight21.Context.UserContext;
using Email = PHSRAG.Insight21.InsightUtilities.Email;
#endregion

namespace PHSRAG.Insight21.InsightWebControls
{
	#region Class InsightDownloadBar
	/// <summary>
	/// The InsightDownloadBar is an extension of the functionality provided by the more generic DownloadBar,
	/// specifically for the Insight application.
	/// </summary>
	/// <remarks>
	/// <protocolId>Along with the parent functions, this control allows Insight detail pages to use the Send by E-mail button correctly.</protocolId>
	/// <protocolId>To include this e-mail functionality into a detail page, a developer must:
	/// <br/>1) Add the InsightDownloadBar control to the actual page
	/// <br/>2) In the code behind, create a handler for the EmailEventHandler delegate as defined in the parent DownloadBar
	///		WebControl and hook it up to the SendByEmail event on the page's InsightDownloadBar
	/// <br/>3) In the handler, set the webConfigURLKey out parameter to the key in the Web.Config file corresponding to a link
	///		to the detail page
	///	<br/>4) In the handler, create a string in the query style (key=value&key=value&...) defining whatever initialization
	///		information the page needs to have in session, using the Keys static class and the CURRENT data the page used to load
	///	<br/>5) Execute the "InsertPageQuery" stored procedure against the database, passing the query string that was created and
	///		store the return value (as int) into the queryId out parameter
	///	<br/>6) Include whatever summary information is required into the summaryInfo out parameter, using the keys provided by the
	///		static Email class in the InsightUtilities project. Refer to the documentation for that class for more info.
	///	<br/>7) If exception handling is implemented in this handler (and it probably should be, as there are several places things
	///		could go wrong), the developer must make sure to set the out parameters to null values (-1 for int) to prevent
	///		further unwanted behavior</protocolId>
	/// </remarks>
	public class InsightDownloadBar : DownloadBar
	{
		#region Constants
		private const string EmailHostScriptKey = "emailHostScript_1C35C472-3924-43c6-AA34-4B7EDFFEBB09";
		#endregion

		#region Static Variables (private)
		// This control can't access the Keys class in the WebSite project, so I have to create local keys here.
		private static string webConfigURLKey = "webConfigURLKey_1C35C472-3924-43c6-AA34-4B7EDFFEBB09";
		private static string queryIdKey = "queryIdKey_1C35C472-3924-43c6-AA34-4B7EDFFEBB09";
		private static string summaryInfoKey = "summaryInfoKey_1C35C472-3924-43c6-AA34-4B7EDFFEBB09";
		private static string emailDataKey = "emailDataKey_1C35C472-3924-43c6-AA34-4B7EDFFEBB09";
		#endregion

		#region Public Properties
		/// <summary>
		/// Gets WebConfigURLKey accessor key for use in e-mail link.
		/// </summary>
		/// <remarks>
		/// This control can't access the Keys class in the WebSite project, so I have to create keys here.
		/// </remarks>
		public static string WebConfigURLKey
		{
			get { return webConfigURLKey; }
		}

		/// <summary>
		/// Gets QueryId accessor key for use in e-mail link.
		/// </summary>
		/// <remarks>
		/// This control can't access the Keys class in the WebSite project, so I have to create keys here.
		/// </remarks>
		public static string QueryIdKey
		{
			get { return queryIdKey; }
		}

		/// <summary>
		/// Gets SummaryInfo accessor key for use in e-mail link.
		/// </summary>
		/// <remarks>
		/// This control can't access the Keys class in the WebSite project, so I have to create keys here.
		/// </remarks>
		public static string SummaryInfoKey
		{
			get { return summaryInfoKey; }
		}
		
		/// <summary>
		/// Gets EmailData accessor key for use in e-mail link.
		/// </summary>
		/// <remarks>
		/// This control can't access the Keys class in the WebSite project, so I have to create keys here.
		/// </remarks>
		public static string EmailDataKey
		{
			get { return emailDataKey; }
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Insight application specific e-mail functionality for use in the DownloadBar WebControl.
		/// Opens a popup page that handles additional e-mail information gathering and sending.
		/// </summary>
		/// <param name="webConfigURLKey">Key in Web.config representing the detail page's URL</param>
		/// <param name="queryId">The querry ID number returned when the calling detail page inserted its query string into the database</param>
		/// <param name="summaryInfo">Additional information about page totals and current fund number</param>
		protected override void SendEmail(string webConfigURLKey, int queryId, Hashtable summaryInfo)
		{
			Hashtable dictionary = new Hashtable();
			dictionary[WebConfigURLKey] = webConfigURLKey;
			dictionary[QueryIdKey] = queryId;
			dictionary[SummaryInfoKey] = summaryInfo;

			Page.Session[EmailDataKey] = dictionary;

			const string features = "width=260,height=320,scrollbars=no,resizable=yes,status=no,location=no,toolbar=no,menubar=no";
			Page.ClientScript.RegisterStartupScript(GetType( ), EmailHostScriptKey,
				string.Format("<script language=\"javascript\">window.open('{0}', 'EmailHost', '{1}');</script>",
				Constants.EmailHostPage, features));
		}
		#endregion
	}
	#endregion
}
// --- EOF ---